<?php
/*
 * @Author: wanzhengxin
 * @Date: 2021-04-01 21:58:34
 */


namespace app\user\controller;


use app\user\model\User as UserModel;  //引入model.
use app\common\controller\DataCrypt;
use app\index\controller\Base;

class User extends Base
{
    /*微信验证登录*/
    public function car_login()
    {
        //开发者使用登陆凭证 code 获取 session_key 和 openid
        $APPID = config('APPID');//自己配置
        $AppSecret =config('AppSecret');//自己配置
        if(empty(input('post.code')) ){
            $data=render(0,'参数缺失');
        }else{
            $code = input('post.code');

            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $APPID . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
            $arr = get_curl_json($url);  // 一个使用curl实现的get方法请求
            return json_encode($arr);
        }

    }
    /*微信解密电话号码*/
    public function getCryptData(){
        $appid =config('APPID');
        $sessionKey = input('post.session_key');
        $encryptedData=input('post.encryptedData');
        $iv = input('post.iv');
        $pc = new DataCrypt($appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );
        if ($errCode == 0) {
            return $data;
        } else {
            return $errCode;
        }
    }
    /**
     * @return mixed
     */
    public function userRegLogin(){
        $data=$this->request->param();//保存获取用户提过来的数据
        $user=new UserModel();
        if(empty($data) ){
            return 0;
        }else{
            $map =['tel'=>$data['tel']];
            return $user->reg($map,$data);
        }
    }
   
    public function get(){
        $user = new UserModel();
        $data=$this->request->param();
        $res1=$user->where($data['field'],$data['op'],$data['value'])
        ->order('status asc,cTime desc ')
        ->limit(10)
        ->page($data['page'])
        ->select();
        return $res1;
    }
    public function update()
    {
        $data = $this->request->param();
        $res = new UserModel();
        return $res->updateModel($data);
    }
    public function delete(){
        $data = $this->request->param();
        $res = new UserModel();
        return $res->deleteModel($data);
    }
    public function save(){
        $res = new UserModel();
        $data = $this->request->param();
        
        $res->saveModel($data);
        return 1;
    }
    public function num(){
        $data = $this->request->param();
        $res =new UserModel();
        return $res->where($data['field'],$data['op'],$data['value'])->count();
    }
    public function proxy(){
        return 'ok';
    }
    //获取二维码
    public function getWxQrcode()
    {
        $data = $this->request->param();
        $model = new UserModel();
        $info = $model->where('u_id', '=', $data['u_id'])->find();
        if (empty($info['QRcode'])) {
            $appid = config('APPID');
            $AppSecret = config('AppSecret'); //自己配置
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $AppSecret;
            $getToken = get_curl_json($url);
            $access_token = $getToken['access_token'];
            $res = send_post('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token, [
                'scene' => $info['u_id'],
                'page' => 'pages/index/index',
                'env_version' => 'develop',
                'width' => 280
            ]);
            $up_dir = "static/sharehis/qrcode" . time() . '.png';
            file_put_contents($up_dir, $res);
            $info->QRcode = $up_dir;
            $info->save();
            return $up_dir;
        }
        return $info['QRcode'];
    }
}